home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 039a / jpsrc2.zip / JWRTARGA.C < prev    next >
C/C++ Source or Header  |  1991-12-12  |  6KB  |  224 lines

  1. /*
  2.  * jwrtarga.c
  3.  *
  4.  * Copyright (C) 1991, Thomas G. Lane.
  5.  * This file is part of the Independent JPEG Group's software.
  6.  * For conditions of distribution and use, see the accompanying README file.
  7.  *
  8.  * This file contains routines to write output images in Targa format.
  9.  *
  10.  * These routines may need modification for non-Unix environments or
  11.  * specialized applications.  As they stand, they assume output to
  12.  * an ordinary stdio stream.
  13.  *
  14.  * These routines are invoked via the methods put_pixel_rows, put_color_map,
  15.  * and output_init/term.
  16.  *
  17.  * Based on code contributed by Lee Daniel Crocker.
  18.  */
  19.  
  20. #include "jinclude.h"
  21.  
  22. #ifdef TARGA_SUPPORTED
  23.  
  24.  
  25. /*
  26.  * To support 12-bit JPEG data, we'd have to scale output down to 8 bits.
  27.  * This is not yet implemented.
  28.  */
  29.  
  30. #ifndef EIGHT_BIT_SAMPLES
  31.   Sorry, this code only copes with 8-bit JSAMPLEs. /* deliberate syntax err */
  32. #endif
  33.  
  34.  
  35. static JSAMPARRAY color_map;    /* saves color map passed by quantizer */
  36.  
  37.  
  38. LOCAL void
  39. write_header (decompress_info_ptr cinfo, int num_colors)
  40. /* Create and write a Targa header */
  41. {
  42.   char targaheader[18];
  43.  
  44.   /* Set unused fields of header to 0 */
  45.   MEMZERO((void *) targaheader, SIZEOF(targaheader));
  46.  
  47.   if (num_colors > 0) {
  48.     targaheader[1] = 1;        /* color map type 1 */
  49.     targaheader[5] = (char) (num_colors & 0xFF);
  50.     targaheader[6] = (char) (num_colors >> 8);
  51.     targaheader[7] = 24;    /* 24 bits per cmap entry */
  52.   }
  53.  
  54.   targaheader[12] = (char) (cinfo->image_width & 0xFF);
  55.   targaheader[13] = (char) (cinfo->image_width >> 8);
  56.   targaheader[14] = (char) (cinfo->image_height & 0xFF);
  57.   targaheader[15] = (char) (cinfo->image_height >> 8);
  58.   targaheader[17] = 0x20;    /* Top-down, non-interlaced */
  59.  
  60.   if (cinfo->out_color_space == CS_GRAYSCALE) {
  61.     targaheader[2] = 3;        /* image type = uncompressed gray-scale */
  62.     targaheader[16] = 8;    /* bits per pixel */
  63.   } else {            /* must be RGB */
  64.     if (num_colors > 0) {
  65.       targaheader[2] = 1;    /* image type = colormapped RGB */
  66.       targaheader[16] = 8;
  67.     } else {
  68.       targaheader[2] = 2;    /* image type = uncompressed RGB */
  69.       targaheader[16] = 24;
  70.     }
  71.   }
  72.  
  73.   if (FWRITE(cinfo->output_file, targaheader, 18) != (size_t) 18)
  74.     ERREXIT(cinfo->emethods, "Could not write Targa header");
  75. }
  76.  
  77.  
  78. /*
  79.  * Write the file header.
  80.  */
  81.  
  82. METHODDEF void
  83. output_init (decompress_info_ptr cinfo)
  84. {
  85.   if (cinfo->out_color_space == CS_GRAYSCALE) {
  86.     /* Targa doesn't have a mapped grayscale format, so we will */
  87.     /* demap quantized gray output.  Never emit a colormap. */
  88.     write_header(cinfo, 0);
  89.   } else if (cinfo->out_color_space == CS_RGB) {
  90.     /* For quantized output, defer writing header until put_color_map time. */
  91.     if (! cinfo->quantize_colors)
  92.       write_header(cinfo, 0);
  93.   } else {
  94.     ERREXIT(cinfo->emethods, "Targa output must be grayscale or RGB");
  95.   }
  96. }
  97.  
  98.  
  99. /*
  100.  * Write some pixel data.
  101.  */
  102.  
  103. METHODDEF void
  104. put_pixel_rows (decompress_info_ptr cinfo, int num_rows,
  105.         JSAMPIMAGE pixel_data)
  106. {
  107.   register FILE * outfile = cinfo->output_file;
  108.   register JSAMPROW ptr0, ptr1, ptr2;
  109.   register long col;
  110.   register long width = cinfo->image_width;
  111.   register int row;
  112.   
  113.   if (cinfo->final_out_comps == 1) {
  114.     /* here for grayscale or quantized color output */
  115.     for (row = 0; row < num_rows; row++) {
  116.       ptr0 = pixel_data[0][row];
  117.       for (col = width; col > 0; col--) {
  118.     putc(GETJSAMPLE(*ptr0), outfile);
  119.     ptr0++;
  120.       }
  121.     }
  122.   } else {
  123.     /* here for unquantized color output */
  124.     for (row = 0; row < num_rows; row++) {
  125.       ptr0 = pixel_data[0][row];
  126.       ptr1 = pixel_data[1][row];
  127.       ptr2 = pixel_data[2][row];
  128.       for (col = width; col > 0; col--) {
  129.     putc(GETJSAMPLE(*ptr2), outfile); /* write in BGR order */
  130.     ptr2++;
  131.     putc(GETJSAMPLE(*ptr1), outfile);
  132.     ptr1++;
  133.     putc(GETJSAMPLE(*ptr0), outfile);
  134.     ptr0++;
  135.       }
  136.     }
  137.   }
  138. }
  139.  
  140.  
  141. /*
  142.  * Write some demapped pixel data when color quantization is in effect.
  143.  * For Targa, this is only applied to grayscale data.
  144.  */
  145.  
  146. METHODDEF void
  147. put_demapped_rows (decompress_info_ptr cinfo, int num_rows,
  148.            JSAMPIMAGE pixel_data)
  149. {
  150.   register FILE * outfile = cinfo->output_file;
  151.   register JSAMPROW ptr;
  152.   register long col;
  153.   register long width = cinfo->image_width;
  154.   register int row;
  155.   
  156.   for (row = 0; row < num_rows; row++) {
  157.     ptr = pixel_data[0][row];
  158.     for (col = width; col > 0; col--) {
  159.       putc(GETJSAMPLE(color_map[0][GETJSAMPLE(*ptr)]), outfile);
  160.       ptr++;
  161.     }
  162.   }
  163. }
  164.  
  165.  
  166. /*
  167.  * Write the color map.
  168.  */
  169.  
  170. METHODDEF void
  171. put_color_map (decompress_info_ptr cinfo, int num_colors, JSAMPARRAY colormap)
  172. {
  173.   register FILE * outfile = cinfo->output_file;
  174.   int i;
  175.  
  176.   if (cinfo->out_color_space == CS_RGB) {
  177.     /* We only support 8-bit colormap indexes, so only 256 colors */
  178.     if (num_colors > 256)
  179.       ERREXIT(cinfo->emethods, "Too many colors for Targa output");
  180.     /* Time to write the header */
  181.     write_header(cinfo, num_colors);
  182.     /* Write the colormap.  Note Targa uses BGR byte order */
  183.     for (i = 0; i < num_colors; i++) {
  184.       putc(GETJSAMPLE(colormap[2][i]), outfile);
  185.       putc(GETJSAMPLE(colormap[1][i]), outfile);
  186.       putc(GETJSAMPLE(colormap[0][i]), outfile);
  187.     }
  188.   } else {
  189.     color_map = colormap;    /* save for use in output */
  190.     cinfo->methods->put_pixel_rows = put_demapped_rows;
  191.   }
  192. }
  193.  
  194.  
  195. /*
  196.  * Finish up at the end of the file.
  197.  */
  198.  
  199. METHODDEF void
  200. output_term (decompress_info_ptr cinfo)
  201. {
  202.   /* No work except to make sure we wrote the output file OK */
  203.   fflush(cinfo->output_file);
  204.   if (ferror(cinfo->output_file))
  205.     ERREXIT(cinfo->emethods, "Output file write error");
  206. }
  207.  
  208.  
  209. /*
  210.  * The method selection routine for Targa format output.
  211.  * This should be called from d_ui_method_selection if Targa output is wanted.
  212.  */
  213.  
  214. GLOBAL void
  215. jselwtarga (decompress_info_ptr cinfo)
  216. {
  217.   cinfo->methods->output_init = output_init;
  218.   cinfo->methods->put_color_map = put_color_map;
  219.   cinfo->methods->put_pixel_rows = put_pixel_rows;
  220.   cinfo->methods->output_term = output_term;
  221. }
  222.  
  223. #endif /* TARGA_SUPPORTED */
  224.